﻿
"use strict";
angular.module('app.project').controller('MyAllProjectCtrl', function ($scope,personalCenterHttp, projectHttpS,jurisHttpS,
		$http,$rootScope, $stateParams,$cookieStore) {

    $scope.paginationConf = {
        currentPage: 1,
        itemsPerPage: 100,
        pagesLength: 5,
        totalItems: 20,
        perPageOptions: [10, 20, 30, 40, 50],
        plan: 0,
        onChange: function () {
        }
    };

    var full_pos = 3000;

    var getData = function(){
        //获得项目数据
        $http.get(baseurls+"/list_nopage/project").then(function(res){
            // console.log(res.data);
            if(res.data.ret == 1){
                $scope.projects = res.data.info;
                for(let i=0;i<$scope.projects.length;i++){
                    // console.log($scope.projects[i]);
                    //项目库存信息
                    $http.get(baseurls+"/detail/inventory?code=="+$scope.projects[i].code,{withCredentials:true}).then(function(detail){
                        // console.log(detail.data.info);
                        if(detail.data.ret == 1){
                            $scope.projects[i].company_name = detail.data.info.company_name;
                            $scope.projects[i].company_id = detail.data.info.company_id;
                            $scope.projects[i].item_code = detail.data.info.item_code;
                            $scope.projects[i].item_name = detail.data.info.item_name;
                            $scope.projects[i].qty = detail.data.info.qty;
                            $scope.projects[i].y_qty = detail.data.info.y_qty;
                            $scope.projects[i].l_qty = detail.data.info.l_qty;
                            $scope.projects[i].unit = detail.data.info.unit;
                            $scope.projects[i].pos_present = (detail.data.info.y_qty/full_pos)*100;
                        }
                    })
                    
                }
                // console.log($scope.projects);
            }
        })
        
    };
    // getData();
    $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage',getData);

    // 项目编辑
    $rootScope.new = false;//删除项目
    $rootScope.save = false;//编辑项目

    $scope.createProject = function () {
        $cookieStore.remove('edit_pid');
        $cookieStore.put('edit_pname', '添加新油罐');
        $rootScope.save = true;
        $rootScope.new = false;
        window.location.href = appConfig.base[0].baseURI + "#/add_newProject";
    }

    $scope.viewDetail = function(pid,pname,pcode){
        // console.log(pid);
        $cookieStore.put('view_pid', pid);
        $cookieStore.put('view_pname', pname);
        $cookieStore.put('view_pcode', pcode);
        window.location.href = appConfig.base[0].baseURI + "#/project_detail";
    }
    $scope.editProject = function(pid,pname,$event){
        $event.stopPropagation();//阻止事件冒泡
        $cookieStore.put('edit_pid', pid);
        $cookieStore.put('edit_pname', pname);

        // $http.get(baseurls+"/session_user").then(function(res){
        //     // console.log(res.data)
        //     if(res.data.fid == 0 && res.data.fid == 1){
        //         $rootScope.new = true;//删除项目
        //     }else{
        //         $rootScope.new = false;//删除项目
        //     }
        //     var power = res.data.power;
        //     if(power=="all"){
        //         $rootScope.new = true;//删除项目
        //         $rootScope.save = true;//编辑项目
        //         // console.log($rootScope.new,$rootScope.save)
        //         window.location.href = appConfig.base[0].baseURI + "#/add_newProject";//编辑项目
        //     }
        //     else{
        //         power = JSON.parse(power);
        //         for(var name in power){
        //             // console.log(power[name])
        //             if(power[name].projectid == pid){
        //                 // console.log(power[name].power,power[name].power & 1)
        //                 if(power[name].power & 1){//不为0 可编辑项目
        //                     $rootScope.save = true;//编辑项目
        //                 }else{
        //                     $rootScope.save = false;
        //                 }
                        
        //             }
        //         }
        //     }
            $rootScope.new = false;//删除项目
            $rootScope.save = false;//编辑项目
            console.log(pid+" "+pname+" 编辑项目"+$rootScope.save+" 删除项目"+$rootScope.new);
            window.location.href = appConfig.base[0].baseURI + "#/add_newProject";//编辑项目
        // })
    }
});